package com.sinoeyes.platform.filter;

import com.alibaba.fastjson.JSONObject;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.R;
import com.sinoeyes.common.utils.StringUtils;
import com.sinoeyes.compent.po.ProvinceInfo;
import com.sinoeyes.compent.service.IProvinceInfoService;
import com.sinoeyes.platform.filter.dto.CurrencyDTO;
import com.sinoeyes.system.service.ISysDictDataService;
import com.sinoeyes.system.vo.SysDictDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所有的页面下拉条件均从这里获取
 *
 * @author aries.hao
 */
@Api(value = "Condition API", tags = {"下拉过滤接口"})
@Slf4j
@RestController
@RequestMapping("/filter")
public class FilterApi extends BaseController {

    private final ISysDictDataService dictDataService;

    private final IProvinceInfoService provinceInfoService;

    @Autowired
    public FilterApi(ISysDictDataService dictDataService, IProvinceInfoService provinceInfoService) {
        this.dictDataService = dictDataService;
        this.provinceInfoService = provinceInfoService;
    }

    @PostMapping(path = "/getDictFilterData")
    @ApiOperation(value = "字典相关filter接口")
    public String getFilterFromDict(@RequestParam(value = "conditions") String conditions) {
        if (StringUtils.isNotBlank(conditions)) {
            String[] conditionArr = conditions.split(",");
            if (conditionArr.length > 0) {
                Map<String, List<CurrencyDTO>> results = new HashMap<>(conditionArr.length);
                for (String condition : conditionArr) {
                    List<SysDictDataVo> sysDictData = dictDataService.selectDictDataByType(condition);
                    List<CurrencyDTO> dtoList = new ArrayList<>(sysDictData.size());
                    for (SysDictDataVo sysDictDatum : sysDictData) {
                        CurrencyDTO dto = new CurrencyDTO();
                        dto.setVal(sysDictDatum.getDictValue());
                        dto.setLable(sysDictDatum.getDictLabel());
                        dtoList.add(dto);
                    }
                    results.put(condition, dtoList);
                }
                if (results.size() > 0) {
                    return convertJsonWithDate(R.success(results));
                }
                return jsonResult(R.failureNull());
            }
        }
        return jsonResult(R.failure());
    }

    @ApiOperation(value = "省份信息接口")
    @RequestMapping(value = "/getProvinces", method = RequestMethod.GET)
    public String getProvincesInfo() {
        List<ProvinceInfo> provinceAll = provinceInfoService.selectAllProvinceInfo();
        Map<String, JSONObject> dataList = new HashMap<>(1);
        if (provinceAll.size() > 0) {
            JSONObject resultObj = new JSONObject(true);
            for (ProvinceInfo provinceInfo : provinceAll) {
                resultObj.put(provinceInfo.getCnname(), provinceInfo.getCnname());
            }
            dataList.put("provinceId", resultObj);
            return convertJsonWithDate(R.success(dataList));
        }

        return jsonResult(R.failureNull());
    }
}
